package com.java2007.hotel.controller.front;

import com.alibaba.druid.util.StringUtils;
import com.java2007.hotel.constant.ServletConstant;
import com.java2007.hotel.controller.Controller;
import com.java2007.hotel.entity.DinnerCar;
import com.java2007.hotel.pojo.DinnerTable;
import com.java2007.hotel.pojo.Order;
import com.java2007.hotel.pojo.OrderDetail;
import com.java2007.hotel.pojo.User;
import com.java2007.hotel.service.DinnerTableService;
import com.java2007.hotel.service.OrderDetailService;
import com.java2007.hotel.service.OrderService;
import com.java2007.hotel.service.impl.DinnerTableServiceImpl;
import com.java2007.hotel.service.impl.OrderDetailServiceImpl;
import com.java2007.hotel.service.impl.OrderServiceImpl;
import com.sun.org.apache.xpath.internal.operations.Or;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**订单操作（订单详情）
 * @author Reckeys
 * @version V1.0
 * @date 2021/01/28
 **/
@WebServlet("/order")
public class OrderController extends Controller {
    private OrderService orderService = new OrderServiceImpl();
    private OrderDetailService orderDetailService = new OrderDetailServiceImpl();
    private DinnerTableService dinnerTableService = new DinnerTableServiceImpl();

    /**添加订单
     * 将session域中的购物车添加到详情订单中
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public String addOrder(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //从session获取到购物车内的数据
        HttpSession session = request.getSession();
        List<DinnerCar> dinnerCarList = (List<DinnerCar>) session.getAttribute("dinnerCarList");
        Long totalPrice = (Long) session.getAttribute("totalPrice");
        Integer tableId = (Integer) session.getAttribute("tableId");
        User user = (User) session.getAttribute("user");
        if(user == null){
            return ServletConstant.jump("未登录","user/login.jsp");
        }

        //判断餐桌状态
        DinnerTable dinnerTable = dinnerTableService.findDinnerTableByTableId(tableId);
        if (dinnerTable.getTableStatus() != 0) {
            return ServletConstant.jump("餐桌已被预定，请更换餐桌", "front?method=findDinnerTableByStatus");
        }
        dinnerTableService.updateDinnerTableStatus(tableId, new Date());

        Integer userId = user.getUserId();
        Map<String, Object> map = orderService.addOrder(dinnerCarList, totalPrice, tableId, userId);
        if (map == null){
            return "添加失败";
        }



        Order order = (Order) map.get("order");
        List<OrderDetail> orderDetailsList = (List<OrderDetail>) map.get("orderDetailsList");
        //将获取的值添加到session域中
        session.setAttribute("order",order);
        session.setAttribute("orderDetailsList",orderDetailsList);

        //删除session域中的购物车信息
        session.removeAttribute("tableId");
        session.removeAttribute("dinnerCarList");
        session.removeAttribute("totalPrice");
        return "redirect:/front/detail/clientOrderList.jsp";
    }

    /**
     * 用于查看历史订单
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String findOrder(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if (user == null){
            return ServletConstant.jump("请登录","user/login.jsp");
        }
        //通过userId查询所有订单
        List<Order> orderList = orderService.findOrderByCreatUserId(user.getUserId());
        session.setAttribute("orderList_history",orderList);
        return "redirect:/front/detail/clientOrderList-history.jsp";
    }

    /**
     * 将详细订单回显
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String findOrderDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String detailOrderIdStr = request.getParameter("detailOrderId");
        if (detailOrderIdStr == null && StringUtils.isEmpty(detailOrderIdStr)){
            return "参数错误";
        }
        Long detailOrderId = Long.valueOf(detailOrderIdStr);
        List<OrderDetail> orderDetailsList = orderDetailService.findOrderDetail(detailOrderId);
        Order order = orderService.findOrder(detailOrderId);
        HttpSession session = request.getSession();
        session.setAttribute("order",order);
        session.setAttribute("orderDetailsList",orderDetailsList);

        return "redirect:/front/detail/clientOrderList.jsp";
    }


}